package oas.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import oas.domain.User;

/**
 * 利用拦截器对访问路径进行管理员登录校验
 *
 * @author 2673
 * @version 1.0
 */
public class ManagerInterceptor implements HandlerInterceptor {

    private final static Log log = LogFactory.getLog(ManagerInterceptor.class);

    /**
     * afterCompletion方法会在一个请求完成之后被调用 。如渲染视图之后。
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object obj,
                                Exception exception) throws Exception {

        log.debug("3.ManagerInerceptor-Called after rendering the view");

    }

    /**
     * postHandle方法会在controller类的处理方法调用之后， 但是在渲染视图之前运行。
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object obj, ModelAndView model)
            throws Exception {

        log.debug("2.ManagerInerceptor-Called after handler method request completion, before rendering the view");

    }

    /**
     * 检测管理员是否登录，未登录不能访问相关界面
     * <p>
     * preHandle会在一个控制器类的处理方法被调用之前执行此方法
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {

        log.debug("1.ManagerInerceptor-Called before handler method");
        // 获取session
        HttpSession session = request.getSession();
        String url = (request.getRequestURI());
        ;
        User user = (User) session.getAttribute("user");
        if (user != null && user.getRole().equals("Manager") || user != null && url.endsWith("center")
                || user != null && url.endsWith("update") || user != null && url.endsWith("password")) {
            return true;
        }
        // 不符合条件的转发到登录页面
        response.sendRedirect(request.getContextPath() + "/login");
        return false;
    }

}
